package com.yuanduo_app.bridge;

import com.facebook.react.bridge.Arguments;
import com.facebook.react.bridge.Callback;
import com.facebook.react.bridge.Promise;
import com.facebook.react.bridge.ReactApplicationContext;
import com.facebook.react.bridge.ReactMethod;
import com.facebook.react.bridge.ReadableMap;
import com.facebook.react.bridge.WritableMap;
import com.google.gson.Gson;
import cn.ydxlm.maplib.MapManager;
import cn.ydxlm.maplib.api.GetSuggestionResultListener;
import cn.ydxlm.maplib.api.LocationListener;
import cn.ydxlm.maplib.entity.Location;
import com.xujl.fastlib.base.BaseModule;

import java.util.List;

import androidx.annotation.NonNull;

public class MapModule extends BaseModule implements LocationListener {
    private Promise promise;
    private Callback suggestionCallback;
    private GetSuggestionResultListener mSuggestionListener = new GetSuggestionResultListener() {
        @Override
        public void onSuggestionResult (List<Location> list) {
            if (suggestionCallback != null) {
                suggestionCallback.invoke(new Gson().toJson(list));
                suggestionCallback = null;
            }
        }
    };

    public MapModule (@NonNull ReactApplicationContext reactContext) {
        super(reactContext);
        MapManager.getInstance().addLocationListener(this);
    }

    @ReactMethod
    public void getCurrentPosition (Promise promise) {
        this.promise = promise;
        MapManager.getInstance().startLocation();
    }

    @ReactMethod
    public void suggestionSearch (ReadableMap map, Callback suggestionCallback) {
        this.suggestionCallback = suggestionCallback;
        MapManager.getInstance().suggestionSearch(getValue("city", map), getValue("keyword", map));
    }

    @ReactMethod
    public void registerSuggestionListener () {
        MapManager.getInstance().addSuggestionResultListener(mSuggestionListener);
    }

    @ReactMethod
    public void unregisterSuggestionListener () {
        MapManager.getInstance().removeSuggestionResultListener(mSuggestionListener);
    }

    @NonNull
    @Override
    public String getName () {
        return "MapModule";
    }

    @Override
    public void onReceiveLocation (Location location) {
        WritableMap params = Arguments.createMap();
        params.putString("province", location.getProvince());
        params.putString("city", location.getCity());
        params.putString("district", location.getArea());
        params.putDouble("latitude", location.getLatitude());
        params.putDouble("longitude", location.getLongitude());
        params.putString("street", location.getStreet());
        params.putString("addressDetail", location.getProvince() + location.getCity() + location.getArea() + location.getStreet());

        params.putDouble("direction", location.getDirection());
        if (promise != null) {
            promise.resolve(params);
            promise = null;
        }
//        sendEvent("onLocationUpdate", params);
    }
}
